package stark.app.function.watermark;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.watermark.lib.video.Parser;
import com.stark.watermark.lib.video.ParserFactory;
import com.stark.watermark.lib.video.callback.ParseCallback;
import com.stark.watermark.lib.video.error.ParseError;
import com.stark.watermark.lib.video.util.UrlUtil;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import stark.app.R;
import stark.app.base.BaseWithFragmentActivity;
import stark.app.databinding.FragmentWatermarkVideoDemoBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.constant.Extra;

/**
 * Author：daizhongbin on 2023/12/15
 * Email：1027154338@qq.com
 * Desc:
 */
public class WatermarkVideoDemoFragment extends BaseNoModelFragment<FragmentWatermarkVideoDemoBinding> {

    private Parser mParser;

    @Override
    protected int onCreate() {
        return R.layout.fragment_watermark_video_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.tvExtractConfirm.setOnClickListener(this);

        mDataBinding.webView.setHtmlCallback((html) -> mParser.parseHtml(html, new ParseCallback() {
            @Override
            public void error(ParseError error) {
                getActivity().runOnUiThread(() -> ToastUtils.showShort(error.getMsg()));
            }

            @Override
            public void success(String url) {
                getActivity().runOnUiThread(() -> {
                    dismissDialog();
                    Bundle bundle = new Bundle();
                    bundle.putString(Extra.PATH, url);
                    BaseWithFragmentActivity.start(mContext, "", WatermarkVideoPlayFragment.class, bundle);
                });
            }
        }));
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvExtractConfirm) {
            String content = mDataBinding.etExtractContent.getText().toString();
            if (TextUtils.isEmpty(content)) {
                ToastUtils.showShort("请输入链接地址");
                return;
            }
            showDialog("视频提取中...");
            String url = UrlUtil.getUrl(content);
            LogUtils.e("短链接：" + url);
            mParser = new ParserFactory().getParser(url);
            CompositeDisposable compositeDisposable = new CompositeDisposable();
            compositeDisposable.clear();
            compositeDisposable.add(Flowable.create((FlowableOnSubscribe<String>) emitter -> {
                        String redirectUrl = UrlUtil.getRedirectUrl(url);
                        emitter.onNext(redirectUrl);
                        emitter.onComplete();
                    }, BackpressureStrategy.BUFFER)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(redirectUrl -> {
                        // 加载网页
                        mDataBinding.webView.loadUrl(redirectUrl);
                    }));
        }
    }
}
